<?php

/*{{{ Download */

/**
 * iDownload 
 * 
 * @category    File Handling
 * @package     Download
 * @version     $id$
 * @copyright   1997-2005 The PHP Group
 * @author      Kazuyoshi Tlacaelel <ktlacaelel@gmail.com> 
 * @license     PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
interface iDownload
{
    public function __construct();
    public function setDownload($filepath);
    public function getRawData();
    public function addHeader($header);
    public function getHeaders();
    public function getStartTime();
    public function getEndTime();
    public function exists();
    public function send();
}

/**
 * Download 
 * 
 * @uses        iDownload
 * @category    File Handling
 * @package     Download
 * @version     $id$
 * @copyright   2007 Kazuyoshi Tlacaelel
 * @author      Kazuyoshi Tlacaelel <ktlacaelel@gmail.com> 
 * @license     PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class Download implements iDownload
{
    protected

    /**
     * filepath 
     * 
     * @var     string
     * @access  protected
     */
    $filepath = '',

    /**
     * headers 
     * 
     * @var     array
     * @access  protected
     */
    $headers = array(),

    /**
     * start_time 
     * 
     * @var     mixed
     * @access  protected
     */
    $start_time,

    /**
     * end_time 
     * 
     * @var     mixed
     * @access  protected
     */
    protected $end_time;

    /**
     * current time getter
     * 
     * @access  protected
     * @return  void
     */
    protected function getTime()
    {
        return microtime();
    }

    /**
     * getRawData 
     * 
     * @access  public
     * @return  string
     */
    public function getRawData()
    {
        return file_get_contents($this->filepath);
    }

    /**
     * __construct 
     * 
     * @access  public
     * @return  void
     */
    public function __construct()
    {
        $this->start_time = $this->getTime();
    }

    /**
     * download definer 
     *
     * defines the file that is to be downloaded
     * 
     * @param   string      $filepath 
     * @access  public
     * @return  void
     */
    public function setDownload($filepath)
    {
        $this->filepath = $filepath;
    }

    /**
     * addHeader 
     * 
     * @param   string      $header 
     * @access  public
     * @return  void
     */
    public function addHeader($header)
    {
        $this->headers[] = $header;
    }

    /**
     * getHeaders 
     * 
     * @access  public
     * @return  string
     */
    public function getHeaders()
    {
        return $this->headers;
    }

    /**
     * getStartTime 
     * 
     * @access  public
     * @return  string
     */
    public function getStartTime()
    {
        return $this->start_time;
    }

    /**
     * getEndTime 
     * 
     * @access  public
     * @return  string
     */
    public function getEndTime()
    {
        return $this->end_time;
    }

    /**
     * determines if the donwload exists 
     * 
     * @access  public
     * @return  boolean
     */
    public function exists()
    {
        return file_exists($this->filepath);
    }

    /**
     * send
     * 
     * @access  public
     * @return  void
     */
    public function send()
    {
        foreach ($this->headers as $header) {
            header($header);
        }
        echo $this->getRawData();
        $this->end_time = $this->getTime();
    }

}

/*}}}*/

?>
